# 定义任务
import logging

from django.conf import settings
from django.core.mail import send_mail

from celery_tasks.Email.Email import EmailCode
from celery_tasks.main import celery_app

logger = logging.getLogger('django')

@celery_app.task(bind=True, name='send_Email_code', retry_backoff=3)
def send_Email_code(self, Email, Email_code):
    """
    发送邮箱验证码的异步任务
    :param Email: 邮箱
    :param Email_code: 邮箱验证码
    :return: True or False
    """
    try:
        EmailCode(Email).send_Email_code(Email_code)  # 发送邮件
    except Exception as e:
        logging.error(e)
        raise self.retry(exec=e,max_retries=3)


@celery_app.task(bind=True, name='send_verify_email', retry_backoff=3)
def send_verify_email(self, to_email, verify_url):
    """
    定义发送邮件 的任务
    :param to_email: 收件人
    :param verify_url: 验证url
    :return:
    """
    try:
        EmailCode(to_email).send_verify_email(verify_url)  # 发送邮件
    except Exception as e:
        logging.error(e)
        raise self.retry(exec=e,max_retries=3)

